package com.sy.service.Impl;

import com.sy.entity.User;
import com.sy.exception.BilibiliException;
import com.sy.mapper.UserMapper;
import com.sy.service.UserService;
import com.sy.vo.UserVo;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

import static com.sy.constant.BusinessConstant.SESSION_CODE_NAME;
import static com.sy.constant.RegexConstant.PHONE_REGEX;
import static com.sy.constant.RegexConstant.STRONG_PWD_REGEX;

/**
 * @author zbbstart
 * @date 2021/1/12 - 18:48
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private HttpServletRequest req;


    @Override
    public boolean regist(UserVo userVo) {
        String userName = userVo.getUserName();
        //1.用户名
        //(1)非空
        if (StringUtils.isBlank(userName)) {
            throw new BilibiliException("用户名不能为空");
        }
        //(2)长度
        if (!(userName.length() >= 2 && userName.length() <= 30)) {
            throw new BilibiliException("用户名长度不正确");
        }

        //(3)重复性校验
        if (null != userMapper.getUserByUserName(userName)) {
            throw new BilibiliException("用户名已经被占用");
        }

        String pwd = userVo.getPassWord();
        //2.密码
        //(1)非空
        if (StringUtils.isBlank(pwd)) {
            throw new BilibiliException("密码不能为空");
        }
        //(2)强度
        if (!pwd.matches(STRONG_PWD_REGEX)) {
            throw new BilibiliException("密码格式不正确");
        }


        String phone = userVo.getUserPhone();
        //4.手机号
        //(1)非空
        if (StringUtils.isBlank(phone)) {
            throw new BilibiliException("手机号不能为空");
        }
        //(2)格式
        if (!phone.matches(PHONE_REGEX)) {
            throw new BilibiliException("手机号格式不正确");
        }
        //(3)重复性校验
        if (null != userMapper.getUserByPhone(phone)) {
            throw new BilibiliException("手机号已经被注册了");
        }
        //5.验证码
        //用户输入的验证码
        String inputCode = userVo.getCode();
        // 获取Session中保存的正确的验证码
        String correctCode = (String) req.getSession().getAttribute(SESSION_CODE_NAME);
        if (StringUtils.isBlank(inputCode)) {
            throw new BilibiliException("请输入验证码");
        }
        if (null == correctCode) {
            throw new BilibiliException("请先获取验证码");
        }
        if (!StringUtils.equalsIgnoreCase(inputCode, correctCode)) {
            throw new BilibiliException("验证码不正确");
        }
        //对密码进行加密
        userVo.setPassWord(DigestUtils.md5Hex(userVo.getPassWord()));
        if (userMapper.regist(userVo)) {
            return true;
        } else {
            throw new BilibiliException("注册失败");
        }
    }

    @Override
    public boolean loginByPwd(String user, String pwd) {
        if (StringUtils.isBlank(user)) {
            throw new BilibiliException("用户名不能为空");
        }
        User user1 = userMapper.getUserByUserName(user);
        if (null == user1) {
            throw new BilibiliException("用户名不存在，请您先去注册");
        }
        if (!StringUtils.equals(DigestUtils.md5Hex(pwd), user1.getPassword())) {
            throw new BilibiliException("密码不正确");
        }

        return true;
    }

    @Override
    public boolean loginByPhone(String phone, String code) {
        //  手机号
        //(1)非空
        if (StringUtils.isBlank(phone)) {
            throw new BilibiliException("手机号不能为空");
        }
        //(2)格式
        if (!phone.matches(PHONE_REGEX)) {
            throw new BilibiliException("手机号格式不正确");
        }
        //(3)重复性校验
        if (null == userMapper.getUserByPhone(phone)) {
            throw new BilibiliException("该手机号未注册");
        }
        //  验证码
        String correctCode = (String) req.getSession().getAttribute(SESSION_CODE_NAME);
        if (StringUtils.isBlank(code)) {
            throw new BilibiliException("请您输入验证码");
        }
        if (null == correctCode) {
            throw new BilibiliException("请获取验证码");
        }
        if (!StringUtils.equalsIgnoreCase(code, correctCode)) {
            throw new BilibiliException("验证码不正确");
        }
        return true;
    }

    @Override
    public User getUserByUserName(String userName) {

        return userMapper.getUserByUserName(userName);
    }

    @Override
    public User getUserByPhone(String phone) {
        return userMapper.getUserByPhone(phone);
    }
}
